package cz.acies.uniql.sql.element;

import org.dom4j.Element;
import org.dom4j.dom.DOMElement;


import cz.acies.uniql.dialect.Dialect;
import cz.acies.uniql.sql.UniQLExpr;

/**
 * The Class UniQLFragment.
 */
public class UniQLFragment extends UniQLExpr
{
	private static final long serialVersionUID = -5271613147509115841L;

	/**
	 * Konstruktor tridy UniQLFragment.
	 */
	public UniQLFragment() {
	}

	/**
	 * Konstruktor tridy UniQLFragment.
	 * 
	 * @param dialect - dialect
	 */
	public UniQLFragment(Dialect dialect) {
		super(dialect);
	}

	/**
	 * Konstruktor tridy UniQLFragment.
	 * 
	 * @param dialect - dialect
	 * @param name - name
	 */
	public UniQLFragment(Dialect dialect, String name) {
		super(dialect, name);
	}

	/**
	 * Konstruktor tridy UniQLFragment.
	 * 
	 * @param name - name
	 */
	public UniQLFragment(String name) {
		super(null, name);
	}
	
	// -----  -----------------------------------------------------------------

	/* (non-Javadoc)
	 * @see cz.acies.uniql.sql.UniQLExpr#translate()
	 */
	public String translate() throws Exception {
		String s = "";
		if (getName() != null && getName().length() > 0)s += getName();
		return s;
	}

	// -----  -----------------------------------------------------------------
	
	/* (non-Javadoc)
	 * @see java.lang.Object#clone()
	 */
	@Override
	public Object clone() throws CloneNotSupportedException {
		Object clone = super.clone();
		assert clone instanceof UniQLFragment;
		return clone;
	}

	// -----  -----------------------------------------------------------------
	
	/* (non-Javadoc)
	 * @see cz.acies.uniql.sql.UniQLExpr#transToXML()
	 */
	public String transToXML() throws Exception {
		DOMElement node = new DOMElement("Alias");
		node.addAttribute("name", this.getName());
		return node.asXML();
	}
	
	/* (non-Javadoc)
	 * @see cz.acies.uniql.sql.UniQLExpr#transToXML(org.dom4j.dom.DOMElement)
	 */
	public DOMElement transToXML(DOMElement node) throws Exception {
		node.setName("UniQLFragment");
		return node;
	}
	
	/* (non-Javadoc)
	 * @see cz.acies.uniql.sql.UniQLExpr#xmlToExpr(java.lang.String)
	 */
	public UniQLExpr xmlToExpr(String xml) throws Exception {
		return this;
	}

	/* (non-Javadoc)
	 * @see cz.acies.uniql.sql.UniQLExpr#xmlToExpr(org.dom4j.Element)
	 */
	public UniQLExpr xmlToExpr(Element node) throws Exception {
		String s1 = node.attributeValue("name");
		if (s1 != null && s1.length() > 0) this.setName(s1);
		return this;
	}
	
	// -----  -----------------------------------------------------------------
	
	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	public String toString() {
		return "UniQLFragment( "+this.getName()+" )";
	}
	/*
	public int hashCode() {
		int hash = -1; 
		if (this.getName() != null) hash += this.getName().hashCode();
		return hash; 
	}
	*/
}
